using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace JobCreateInDocs2.Features.JobCreateInDocs2
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("f19bd9ae-b5bb-431a-adf1-a2039ec93eaf")]
    public class JobCreateInDocs2EventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    // delete preview job item
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (
                               job.Name == "JobCreateInDocs_v2"
                            || job.Name == "JobCreateInDocs_v8"
                            || job.Name == "JobCreateInDocs_v12"
                            || job.Name == "JobCreateInDocs_v14"
                            )
                        {
                            job.Delete();
                        }
                    }
                    JobCreateInDocs listLoggerJob =
                        new JobCreateInDocs("JobCreateInDocs_v14", site.WebApplication);
                    SPMinuteSchedule schedule = new SPMinuteSchedule();
                    schedule.BeginSecond = 0;
                    schedule.EndSecond = 59;
                    schedule.Interval = 20;
                    //SPSchedule schedule = SPSchedule.FromString("every 20 minutes between 7:00:00 and 23:00:00");
                    listLoggerJob.Schedule = schedule;
                    listLoggerJob.Update();
                }
            });
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (job.Name == "JobCreateInDocs_v2"
                            || job.Name == "JobCreateInDocs_v8"
                            || job.Name == "JobCreateInDocs_v12"
                            || job.Name == "JobCreateInDocs_v14"
                            )
                        {
                            job.Delete();
                        }
                    }
                }
            });
        }

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}

